package cn.ubest.mvvmcore.base

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.annotation.StyleRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.ViewModelProvider
import cn.ubest.mvvmcore.BR
import java.lang.reflect.ParameterizedType

/**
 * Created by maoqi on 2020/9/23.
 */
abstract class BaseDialogFragment<B : ViewDataBinding> : DialogFragment() {
    var mBinding: B? = null

    abstract fun initLayout(): Int
    abstract fun initData(arguments: Bundle?)
    abstract fun initView(view: View)
    abstract fun loadData()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val rootView = inflater.inflate(initLayout(), null)
        mBinding = DataBindingUtil.bind<B>(rootView)!!
        return rootView;
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding?.lifecycleOwner = this
        initData(arguments)
        initView(view)
        loadData()
    }

    /**
     * 禁止Dialog取消，包括点击外部和返回键,可重写initDialog方法，在里面调用
     *
     * @param dialog
     */
    protected open fun prohibitCancel(dialog: Dialog) {
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        dialog.setOnKeyListener { _, keyCode, _ ->
            keyCode == KeyEvent.KEYCODE_BACK
        }
    }

    protected open fun initDialog(dialog: Dialog) {
        dialog.setCanceledOnTouchOutside(true)
        dialog.setCancelable(true)
    }

    /**
     * 设置背景色和dialog位置，默认居中
     *
     * @param window
     */
    protected open fun initWindow(window: Window) {
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window.setGravity(initGravity())
        if (initDialogAnimation() != -1) {
            window.setWindowAnimations(initDialogAnimation())
        }
    }

    /**
     * 设置dialog进入退出动画
     *
     * @return
     */
    @StyleRes
    protected open fun initDialogAnimation(): Int {
        return -1
    }

    /**
     * dialog位置，默认居中
     *
     * @return
     */
    protected open fun initGravity(): Int {
        return Gravity.CENTER
    }

    private var width = ViewGroup.LayoutParams.WRAP_CONTENT
    private var height = ViewGroup.LayoutParams.WRAP_CONTENT

    protected open fun setWidth(width: Int) {
        this.width = width
    }

    protected open fun setHeight(height: Int) {
        this.height = height
    }

    override fun onStart() {
        super.onStart()
        dialog?.also {
            initDialog(it)
            it.window?.also { window ->
                initWindow(window)
                window.setLayout(width, height)
            }
        }
    }

    fun finishSelf() {
        activity?.also {
            it.supportFragmentManager.beginTransaction().remove(this).commitAllowingStateLoss()
        }
    }
}